package org.Panel;

import com.google.gson.Gson;
import org.JsonMsg.PersonalMsg;
import org.Tools.OnLineOP;
import org.implement.Panel.PanelSize;
import org.implement.Panel.PanelSkip;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.util.ArrayList;

public class LoginPanel extends FaPanel{
    //用户名
    JLabel lbusername = new JLabel("用户名：");
    JTextField username;
    //密码
    JLabel lbuserpass = new JLabel("密码：");
    JPasswordField password;
    //登陆按钮
    JButton login = new JButton("登陆");
    //提示文本
    JLabel Tip = new JLabel();
    //密码框显现按钮
    JButton vis = new JButton("N");
    //密码显现的标识
    boolean isShowPass = false;
    //返回按钮
    JButton returnButton = new JButton("返回");
    //json数据操作工具
    Gson gson = new Gson();
    //母控件大小接口
    PanelSize panelSize;
    //母控件跳转接口
    PanelSkip panelSkip;
    //控件列表
    ArrayList<JComponent> components = new ArrayList<>();


    public LoginPanel(PanelSize panelSize1, PanelSkip panelSkip1) {

        this.panelSize = panelSize1;
        this.panelSkip = panelSkip1;
        //界面属性设置
        PanelIndex = 1; //界面索引

        setBounds(0,0,panelSize.getWidth(),panelSize.getHeight());
        setLayout(null);

        //用户名
        lbusername.setBounds(275,135,100,80);
        components.add(lbusername);
        username = new JTextField(20);
        username.setBounds(425,135,500,80);
        components.add(username);

        //密码
        lbuserpass.setBounds(275,285,100,80);
        components.add(lbuserpass);
        password = new JPasswordField(20);
        password.setBounds(425,285,500,80);
        password.setEchoChar('*');  //设置密码隐藏后的字符
        components.add(password);

        //登陆按钮
        login.setBounds(350,550,500,100);
        components.add(login);
        login.addMouseListener(new MouseAdapter() {
            //用于判定鼠标是否在按钮内
            int f=1;
            @Override
            public void mouseClicked(MouseEvent e) {}

            @Override
            public void mousePressed(MouseEvent e) {}

            @Override
            public void mouseReleased(MouseEvent e) {
                if(f==1){
                    OnLineOP onLineOP = new OnLineOP();
                    //判断账号密码是否正确
                    String usernametxt = username.getText();
                    String passwordtxt = password.getText();
                    String msg = "login-{\"username\":\""+usernametxt+"\",\"passwordHash\":\""+passwordtxt+"\"}-svc";
                    System.out.println(msg);
                    String jsonmsg = "";
                    try {
                        jsonmsg=onLineOP.sendone(msg);
                    } catch (IOException ex) {
                        throw new RuntimeException(ex);
                    }
                    System.out.println(jsonmsg);
                    if(jsonmsg.equals("false")) {
                        Tip.setText("登陆失败");
                        Tip.setForeground(Color.RED);
                    } else {
                        Tip.setText("登陆成功");
                        Tip.setForeground(Color.GREEN);
                        MainPanel.userName = usernametxt;
                        //登陆后反馈的个人信息
                        PersonalMsg personalMsg = gson.fromJson(jsonmsg, PersonalMsg.class);
                        MainPanel.userId = personalMsg.getId();
                        if(personalMsg.getRole().equals("USER")) {
                            MainPanel.userPower = "USER";
                        }else {
                            MainPanel.userPower = "ADMIN";
                        }

                        //发送给服务器的数据
                        msg = "getAllPost-a-"+MainPanel.userName;
                        //服务器端获取的数据
                        try {
                            jsonmsg = onLineOP.sendone(msg);
                        } catch (IOException ex) {
                            throw new RuntimeException(ex);
                        }
                        try {
                            jsonmsg = onLineOP.sendone(msg);
                        } catch (IOException ex) {
                            throw new RuntimeException(ex);
                        }
                        MainPanel.AllPost = jsonmsg;


                        panelSkip.skip(4);
                    }

                }
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                f=1;
            }

            @Override
            public void mouseExited(MouseEvent e) {
                f=0;
            }
        });

        //提示文本
        Tip.setBounds(400,450,400,50);
        Tip.setForeground(Color.RED);   //设置提示文本的字体颜色为红色
        components.add(Tip);

        //密码显示按钮
        vis.setBounds(950,285,80,80);
        components.add(vis);
        vis.addMouseListener(new MouseAdapter() {
            //用于判定鼠标是否在按钮内
            int f=1;
            @Override
            public void mouseClicked(MouseEvent e) {}

            @Override
            public void mousePressed(MouseEvent e) {}

            @Override
            public void mouseReleased(MouseEvent e) {
                if(f==1) {
                    isShowPass=!isShowPass;
                }
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                f=1;
            }

            @Override
            public void mouseExited(MouseEvent e) {
                f=0;
            }
        });

        //返回按钮
        returnButton.setBounds(5,5,80,50);
        returnButton.addMouseListener(new MouseAdapter() {
            //用于判定鼠标是否在按钮内
            int f=1;
            @Override
            public void mouseClicked(MouseEvent e) {}

            @Override
            public void mousePressed(MouseEvent e) {}

            @Override
            public void mouseReleased(MouseEvent e) {
                if(f==1) {
                    panelSkip.skip(0);
                }
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                f=1;
            }

            @Override
            public void mouseExited(MouseEvent e) {
                f=0;
            }
        });


        //添加控件
        add(lbusername);
        add(username);
        add(lbuserpass);
        add(password);
        add(login);
        add(Tip);
        add(vis);
        add(returnButton);

    }

    @Override
    public void Run() {
        //控件属性更新
        ComponentUpdate();

        //显示密码按钮判断
        if(isShowPass) {
            password.setEchoChar('\0');
            vis.setText("Y");
        }else {
            password.setEchoChar('*');
            vis.setText("N");
        }
    }

    private void ComponentUpdate() {
        //控件属性更新
        setBounds(0,0,panelSize.getWidth(),panelSize.getHeight());
        lbusername.setBounds(275*getWidth()/1200,135*getHeight()/800,100*getWidth()/1200,80*getHeight()/800);
        username.setBounds(425*getWidth()/1200,135*getHeight()/800,500*getWidth()/1200,80*getHeight()/800);
        lbuserpass.setBounds(275*getWidth()/1200,285*getHeight()/800,100*getWidth()/1200,80*getHeight()/800);
        password.setBounds(425*getWidth()/1200,285*getHeight()/800,500*getWidth()/1200,80*getHeight()/800);
        login.setBounds(350*getWidth()/1200,550*getHeight()/800,500*getWidth()/1200,100*getHeight()/800);
        Tip.setBounds(400*getWidth()/1200,450*getHeight()/800,400*getWidth()/1200,50*getHeight()/800);
        vis.setBounds(950*getWidth()/1200,285*getHeight()/800,80*getWidth()/1200,80*getHeight()/800);
//        System.out.println(getWidth());
//        System.out.println(getHeight());
    }
}
